<?php

namespace apiend\modules\work\models;

use Yii;
use ReflectionClass;
use yii\helpers\ArrayHelper;
use common\helpers\Url;
use yii\helpers\Inflector;

/**
 * SurveySearch is the model class for table "{{%book}}".
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SurveyViewSearch extends \common\models\survey\Survey {

    /**
     * 是否需要/可补全进行积分交易订单
     * @return array
     */
    public function getIsExpired() {
        return $this->date && $this->date < date('Y-m-d');
    }

    /**
     * 是否可审核
     * @return array
     */
    public function getCanAudit() {
        if ($this->isExpired) {
            return false;
        }
        return $this->status == static::STATUS_ACTIVE || $this->status == static::STATUS_PENDDING;
    }

    /**
     * 是否需要/可补全进行积分交易订单
     * @return array
     */
    public function getCanUpdate() {
        if ($this->isExpired) {
            return false;
        }
        if ($this->status >= static::STATUS_ACTIVE && $this->status < static::STATUS_COMPLETE) {
            return true;
        }
        return false;
    }

    /**
     * 是否可以校准订单
     * @return array
     */
    public function getCanDelete() {
        return $this->status == static::STATUS_CANCEL;
    }

    /**
     * 当前交易可执行操作
     * @return array
     */
    public function getHandles() {
        $currentMethodName = str_replace(__CLASS__ . '::', '', __METHOD__);
        $ref = new ReflectionClass(__CLASS__);
        $handleActions = array_filter($ref->getMethods(), function($property) use ($currentMethodName) {
            if ($property->class !== __CLASS__) {
                return false;
            }
            if ($property->name === $currentMethodName) {
                return false;
            }
            if (strpos($property->name, 'getCan') !== 0) {
                return false;
            }
            return true;
        });
        $handles = [];
        foreach ($handleActions as $action) {
            if (!$this->{$action->name}()) {
                continue;
            }
            $propertyName = lcfirst(str_replace('getCan', '', $action->name));
            $name = Inflector::underscore($propertyName);
            $text = $this->getAttributeLabel($propertyName);
            $handles[] = compact('name', 'text');
        }
        return $handles;
    }

    public function attributeLabels() {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'delete' => '删除',
            'update' => '修改',
            'audit' => '审核',
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'user_id',
            'company_id',
            'company_name',
            'contact',
            'contact_mobile',
            'date',
            'room_id',
            'room',
            'stages' => 'bookStages',
            'waiterDepartment',
            'waiter_id',
            'waiter_name',
            'numbers',
            'site',
            'mark',
            'status' => function() {
                return $this->getMarkLabel('status');
            },
            'staffs',
            'categorys',
            'created_at',
            'updated_at',
            'deleted_at',
            'handles',
            'download' => function() {
                return $this->status >= 0;
            },
        ];
    }

}
